import random

from selenium.webdriver.support.wait import WebDriverWait

from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import StaleElementReferenceException
import time,pytest
from Case.python_ui.PageLocate.new_ticket_locator import NewTicketLocator
from Case.python_ui.PageLocate.user_my_tickets_locator import UserMyTicketLocator


class NewTicketPage(Basepage):

    """
        公共方法
    """
    def clear_ticket_id(self):
        doc = '清空Ticket No查询'
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.search_box,doc)
        self.click_element(UserMyTicketLocator.search_box,doc)
        self.keyboard_event_all_select(UserMyTicketLocator.search_box,doc)
        self.keyboard_event(UserMyTicketLocator.search_box,doc)
        self.click_element(UserMyTicketLocator.search_button,doc)
        time.sleep(10)

    """"""

    ticket_category_hotfix = {
        "Management And Monitoring":"CloudMonitor",
        "Elastic Calculation":"Container Registry,Container Service for Kubernetes (ACK),Function Compute,Elastic Compute Service,Auto Scaling",
        "Data Base":"ApsaraDB for RDS,ApsaraDB for MongoDB,ApsaraDB for Redis",
        "More":"Resource Orchestration Service,Log Service,Express Connect",
        "Storage":"Network Attached Storage,Object Storage Service",
        "Express Connect":"Express Connect",
        "Network":"Server Load Balancer,Virtual Private Cloud,Elastic IP Addresses,NAT Gateways,VPN Gateways,High-availability Virtual IP Address"
    }
    ticket_category = {
        "Developer Services": "CloudMonitor",
        "Compute": "Container Registry,Container Service for Kubernetes (ACK),Function Compute,Elastic Compute Service,Auto Scaling",
        "Database": "ApsaraDB for RDS,ApsaraDB for MongoDB,ApsaraDB for Redis",
        "More": "Resource Orchestration Service,Log Service,Express Connect",
        "Storage": "Network Attached Storage,Object Storage Service",
        "Express Connect": "Express Connect",
        "Network": "Server Load Balancer,Virtual Private Cloud,Elastic IP Addresses,NAT Gateways,VPN Gateways,High-availability Virtual IP Address"
    }
    ticket_new_category = {
        "Network":{
            "Elastic IP Addresses":"Purchase/Renewal/Billing/Invoice/Cancellation,EIP Modification,EIP Access,EIP Binding/Unbinding,Others",
            "NAT Gateways":"Public NAT SNAT Access,Public NAT DNAT Access,Abnormal Access with NAT Gateway SNAT,Abnormal Access with NAT Gateway DNAT,Purchase/Renewal/Billing/Invoice/Cancellation,NAT Gateway Modification,Others",
            "VPN Gateways":"IPsec-VPN Tunnel Negotiation Failure,IPsec-VPN Tunnel Negotiation Success but No Forwarding,SSL-VPN Connection Failure,SSL-VPN Connection Success but No Forwarding,IPsec Server Issues,Others",
            "High-availability Virtual IP Address":"Technical Support"
        },
        "Database":{
            "ApsaraDB for Redis":"Technical Support"
        },
        "Data Base": {
            "ApsaraDB for Redis": "Technical Support"
        }
    }
    ticket_new_category_network_list = ["Elastic IP Addresses","NAT Gateways","VPN Gateways","High-availability Virtual IP Address"]

    #进入new ticket菜单页面
    def  enter_the_new_ticket_page(self):
        time.sleep(2)
        doc = "进入new ticket菜单"
        for i in range(3):
            try:
                self.scroll_bar_operation(NewTicketLocator.ticket_tab)
                time.sleep(2)
                self.wait_elevisble(NewTicketLocator.ticket_tab,doc)
                self.click_element(NewTicketLocator.ticket_tab,doc)
                self.wait_elevisble(NewTicketLocator.new_ticket_tab,doc)
                self.click_element(NewTicketLocator.new_ticket_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue




    #创建Financial Support ticket
    def create_ticket(self):
        self.enter_the_new_ticket_page()
        time.sleep(2)
        doc = "创建Financial Support ticket"
        ticket_des = "auto_test_financial_" + str(random.randint(1, 1000))
        self.click_element(NewTicketLocator.add_financial_support_ticket,doc)
        time.sleep(3)
        self.wait_elevisble(NewTicketLocator.ticket_description,doc)
        self.send_keys(NewTicketLocator.ticket_description, ticket_des,doc)
        self.wait_elevisble(NewTicketLocator.ticket_additional,doc)
        self.send_keys(NewTicketLocator.ticket_additional, ticket_des,doc)
        self.wait_elevisble(NewTicketLocator.ticket_show_current_issun_list,doc)
        self.click_element(NewTicketLocator.ticket_show_current_issun_list,doc)
        time.sleep(2)
        self.wait_elevisble(NewTicketLocator.ticket_current_issue_list,doc)
        self.click_element(NewTicketLocator.ticket_current_issue_list,doc)
        self.wait_elevisble(NewTicketLocator.ticket_submit, doc)
        self.click_element(NewTicketLocator.ticket_submit,doc)
        time.sleep(3)
        for i in range(3):
            try:
                self.wait_elevisble(NewTicketLocator.ticket_status_filter, doc)
                #self.wait_elevisble(NewTicketLocator.last_ticket, doc)
                break
            except:
                self.wait_elevisble(NewTicketLocator.ticket_submit, doc)
                self.click_element(NewTicketLocator.ticket_submit, doc)
                time.sleep(10)
                continue
        self.clear_ticket_id()  # 清空选项
        self.search_all_ticket()
        for i in range(3):
            try:
                self.wait_elevisble(NewTicketLocator.last_ticket, doc)
                break
            except:
                time.sleep(10)
                continue
        lats_ticket = self.get_element(NewTicketLocator.last_ticket, doc).text
        assert lats_ticket == ticket_des
        lats_ticket_id = self.get_element(NewTicketLocator.last_ticket_id, doc).text
        return lats_ticket_id


    # 创建Compute ticket
    def create_compute_ticket(self):
        self.enter_the_new_ticket_page()
        time.sleep(2)
        doc = "创建Compute ticket"
        ticket_des = "auto_test_compute_" + str(random.randint(1, 1000))
        self.click_element(NewTicketLocator.add_compute_ticket,doc)
        time.sleep(5)
        self.wait_elevisble(NewTicketLocator.ticket_description,doc)
        self.send_keys(NewTicketLocator.ticket_description, ticket_des,doc)
        self.wait_elevisble(NewTicketLocator.ticket_additional,doc)
        self.send_keys(NewTicketLocator.ticket_additional, ticket_des,doc)
        self.wait_elevisble(NewTicketLocator.sub_product,doc)
        self.click_element(NewTicketLocator.sub_product,doc)
        self.wait_elevisble(NewTicketLocator.ticket_sub_pro_com,doc)
        self.click_element(NewTicketLocator.ticket_sub_pro_com,doc)

        self.wait_elevisble(NewTicketLocator.ticket_show_current_issun_list_com,doc)
        self.click_element(NewTicketLocator.ticket_show_current_issun_list_com,doc)
        self.wait_elevisble(NewTicketLocator.ticket_current_issue_list_com,doc)
        self.click_element(NewTicketLocator.ticket_current_issue_list_com,doc)

        self.wait_elevisble(NewTicketLocator.priority_level,doc)
        self.click_element(NewTicketLocator.priority_level,doc)
        self.wait_elevisble(NewTicketLocator.priority_level_low,doc)
        self.click_element(NewTicketLocator.priority_level_low,doc)

        self.click_element(NewTicketLocator.ticket_submit,doc)
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(NewTicketLocator.ticket_status_filter, doc)
                break
            except:
                self.wait_elevisble(NewTicketLocator.ticket_submit, doc)
                self.click_element(NewTicketLocator.ticket_submit, doc)
                time.sleep(10)
                continue
        self.search_all_ticket()
        for i in range(3):
            try:
                self.wait_elevisble(NewTicketLocator.last_ticket, doc)
                break
            except:
                time.sleep(10)
                continue
        lats_ticket = self.get_element(NewTicketLocator.last_ticket,doc).text
        assert lats_ticket == ticket_des
        lats_ticket_id = self.get_element(NewTicketLocator.last_ticket_id, doc).text
        return lats_ticket_id

    def get_sub_product_list(self,name,cmdopt):
        if cmdopt == "hotfix":
            list = self.ticket_category_hotfix[name]
            list = list.split(",")
            return list
        else:
            list = self.ticket_category[name]
            list = list.split(",")
            return list

    def get_current_issue_list(self,name):
        list = self.ticket_new_category['Network'][name]
        new_list = list.split(",")
        return new_list

    def check_ticket_category(self,cmdopt):
        self.enter_the_new_ticket_page()
        time.sleep(2)
        doc = "检查工单目录"
        a = 4
        while a < 11:
            self.wait_elevisble(NewTicketLocator.get_create_button(self,a),doc)
            self.click_element(NewTicketLocator.get_create_button(self,a), doc)
            time.sleep(2)
            self.wait_elevisble(NewTicketLocator.product_name,doc)
            ticket_category_name = self.get_element(NewTicketLocator.product_name, doc).text
            list = self.get_sub_product_list(ticket_category_name,cmdopt)
            self.wait_elevisble(NewTicketLocator.sub_product, doc)
            self.click_element(NewTicketLocator.sub_product, doc)
            for i in list:
                print(">>>>>>>>>>>>等待", i)
                self.wait_elevisble(NewTicketLocator.get_sub_product_locate(self,i),doc)
            self.wait_elevisble(NewTicketLocator.back_button,doc)
            self.click_element(NewTicketLocator.back_button,doc)
            a = a + 1

    def check_new_ticket_category(self):
        self.enter_the_new_ticket_page()
        time.sleep(2)
        doc = "检查新增的工单目录"
        a = 4
        while a < 11:
            self.wait_elevisble(NewTicketLocator.get_category_name(self,a),doc)
            name = self.get_element(NewTicketLocator.get_category_name(self,a),doc).text
            if name == "Network":
                self.wait_elevisble(NewTicketLocator.get_create_button(self, a), doc)
                self.click_element(NewTicketLocator.get_create_button(self, a), doc)
                time.sleep(2)
                b = 0
                while b < 4:
                    self.wait_elevisble(NewTicketLocator.sub_product, doc)
                    self.click_element(NewTicketLocator.sub_product, doc)
                    self.wait_elevisble(NewTicketLocator.get_sub_product_locate(self,self.ticket_new_category_network_list[b]),doc)
                    self.click_element(NewTicketLocator.get_sub_product_locate(self,self.ticket_new_category_network_list[b]),doc)
                    list = self.get_current_issue_list(self.ticket_new_category_network_list[b])
                    time.sleep(2)
                    self.wait_elevisble(NewTicketLocator.ticket_show_current_issun_list_com, doc)
                    self.click_element(NewTicketLocator.ticket_show_current_issun_list_com, doc)
                    for i in list:
                        print(">>>>>>>>>>>>等待", i)
                        self.wait_elevisble(NewTicketLocator.get_sub_product_locate(self, i), doc)
                    b = b + 1
                self.wait_elevisble(NewTicketLocator.new_ticket_tab, doc)
                self.click_element(NewTicketLocator.new_ticket_tab, doc)
            if name in "Database Data Base":
                self.wait_elevisble(NewTicketLocator.get_create_button(self, a), doc)
                self.click_element(NewTicketLocator.get_create_button(self, a), doc)
                time.sleep(2)
                self.wait_elevisble(NewTicketLocator.sub_product, doc)
                self.click_element(NewTicketLocator.sub_product, doc)
                locate = NewTicketLocator.redis
                self.wait_elevisble(locate, doc)
                self.click_element(locate, doc)
                self.wait_elevisble(NewTicketLocator.ticket_show_current_issun_list_com, doc)
                self.click_element(NewTicketLocator.ticket_show_current_issun_list_com, doc)
                self.wait_elevisble(NewTicketLocator.get_sub_product_locate(self, "Technical Support"), doc)
                self.wait_elevisble(NewTicketLocator.new_ticket_tab, doc)
                self.click_element(NewTicketLocator.new_ticket_tab, doc)
            a = a + 1




    def test(self):

       a =  self.ticket_new_category['Network'][self.ticket_new_category_network_list[1]]
       print(">>>>>>>>>>>>>>>>>>>>>",a)



    #Ticket展示所有工单
    def search_all_ticket(self):
        doc = "User My Ticket展示所有工单"
        self.wait_elevisble(UserMyTicketLocator.ticket_priority_filter, doc)
        NewTicketPage.clear_ticket_id(self)
        for i in range(6):
            try:
                self.click_element(UserMyTicketLocator.ticket_priority_filter, doc)
                break
            except:
                time.sleep(10)
                continue
        self.wait_elevisble(UserMyTicketLocator.priority_reset, doc)
        self.click_element(UserMyTicketLocator.priority_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.priority_ok, doc)
        self.click_element(UserMyTicketLocator.priority_ok, doc)
        time.sleep(4)
        js_object = 'document.getElementsByClassName("antd4-table-content")[0].scrollTo(1000,10000)'
        self.excute_js(js_object);
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.ticket_status_filter, doc)
        self.click_element(UserMyTicketLocator.ticket_status_filter, doc)
        self.wait_elevisble(UserMyTicketLocator.status_reset, doc)
        self.click_element(UserMyTicketLocator.status_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.status_ok, doc)
        self.click_element(UserMyTicketLocator.status_ok, doc)
        time.sleep(2)
